package com.dch;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dch.Model;

public class MyServlet extends HttpServlet{
	@Override
	 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
	   throws ServletException, IOException {
	  doGet(req,resp);
	 }
	
	@Override
	 public void doGet(HttpServletRequest req, HttpServletResponse resp)
	   throws ServletException, IOException {
	  HttpSession session = req.getSession();
	  Model account = new Model();
	  
	  String username = req.getParameter("username");
	  String usernames = req.getParameter("usernames");
	  String pwd = req.getParameter("password");
	  String pwdagain = req.getParameter("passwordagain");
	  String email = req.getParameter("email");
	  String address = req.getParameter("address");
	  
	
	  
	  account.setPassword(pwd);
	  account.setUsername(username);
	  account.setAddress(address);
	  account.setEmail(email);
	  account.setPasswordagain(pwdagain);
	  account.setUsernames(usernames);
	  
	  String str = "insert into userinfo(username,address,email,usernames,password) values('"+username+"','"+address+"','"+email+"','"+usernames+"','"+pwd+"')";
	  Connection conn = null;
	   Statement st = null;
	   
	   try
	   {
	    Class.forName("com.mysql.jdbc.Driver");
	   }
	   catch(Exception ee)
	   {
	    ee.printStackTrace();
	   }
	   try
	   {
	    conn = (Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/dch","root","123");
	    st = conn.createStatement();
	    st.executeUpdate(str);
	   }
	   catch(Exception eo)
	   {
	    eo.printStackTrace();
	   
	}
	  
	  session.setAttribute("account", account);

	  resp.sendRedirect("12.jsp");
	  return;

	 }

}
